/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cjava.system.bean.user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import org.cjava.system.model.Company;
import org.cjava.system.model.SystemUser;
import org.cjava.system.service.interfac.ICompanyService;
import org.cjava.system.service.interfac.ISystemUserService;
import org.cjava.system.util.Clazz;
import org.cjava.system.util.faces.FacesUtil;

/**
 *
 * @author user
 */
@ManagedBean
@SessionScoped
public class SystemUserBean implements Serializable {
private static final long serialVersionUID = 1L;
    @ManagedProperty("#{systemUserService}")
    ISystemUserService systemUserService;
    @ManagedProperty("#{companyService}")
    ICompanyService companyService;
    /////////////////////////////////////////////////
    private SystemUser systemUser = new SystemUser();
    private SystemUser systemUserCompany = new SystemUser();
    private List<String> listTypes;
    private String typeCompany = Clazz._TYPE_SYSTEM_USER._EMPRESA;
    private List<Company> listCompany;
    private Company company = new Company();
    /////////////////////////////////////////////////
    private List<SystemUser> listSystemUser;
    private List<SystemUser> listSystemUserCompany;
    //////////////////////////////////////////////
    

    public void initValues() {
        listCompany = null;
        company = new Company();
        listSystemUser = null;
        listSystemUserCompany = null;
        systemUser = new SystemUser();
        systemUserCompany = new SystemUser();
        systemUserCompany.setType(Clazz._TYPE_SYSTEM_USER._EMPRESA);
    }

    public void saveOrUpdateSystemUserCompany() throws Exception {
        if (systemUserCompany.getId() == null) {
            systemUserCompany.setId(systemUserService.save(systemUserCompany));
            if( company != null ){
                company.setSystemUserId(systemUserCompany);
                companyService.update(company);
            }
            FacesUtil.addMessage("Guardado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValues();
        } else {
            systemUserService.update(systemUserCompany);
            if( company != null ){
                company.setSystemUserId(systemUserCompany);
                companyService.update(company);
            }
            FacesUtil.addMessage("Editado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValues();
        }
    }

    public void saveOrUpdateSystemUser() throws Exception {
        if (systemUser.getId() == null) {
            systemUserService.save(systemUser);
            FacesUtil.addMessage("Guardado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValues();
        } else {
            systemUserService.update(systemUser);
            FacesUtil.addMessage("Editado con éxito", null, FacesMessage.SEVERITY_INFO);
            initValues();
        }
    }

    public void saveOrUpdateCompany() throws Exception {
        if( company.getId() == null ) {
            companyService.save(company);
            listCompany.add(company);
            FacesUtil.addMessage("Guardado con éxito", null, FacesMessage.SEVERITY_INFO);
            company = new Company();
        } else {
            companyService.update(company);
            listCompany = null;
            company = new Company();
            FacesUtil.addMessage("Actualizado con éxito", null, FacesMessage.SEVERITY_INFO);
        }  
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    public SystemUser getSystemUser() {
        return systemUser;
    }

    public SystemUser getSystemUserCompany() {
        return systemUserCompany;
    }

    public List<Company> getListCompany() throws Exception {
        if (listCompany == null) {
            listCompany = companyService.listHQL("from Company c "
                    + "where c.systemUserId.type='" + Clazz._TYPE_SYSTEM_USER._EMPRESA + "'");
        }
        return listCompany;
    }

    public void setListCompany(List<Company> listcompany) {
        this.listCompany = listcompany;
    }

    public void setSystemUserCompany(SystemUser systemUserCompany) {
        this.systemUserCompany = systemUserCompany;
    }

    public List<SystemUser> getListSystemUserCompany() throws Exception {
        if (listSystemUserCompany == null) {
            String hql = "from SystemUser s where "
                    + "s.type='" + Clazz._TYPE_SYSTEM_USER._EMPRESA + "' ";
            listSystemUserCompany = systemUserService.listHQL(hql);
        }
        return listSystemUserCompany;
    }

    public void setListSystemUserCompany(List<SystemUser> listSystemUserCompany) {
        this.listSystemUserCompany = listSystemUserCompany;
    }

    public List<String> getListTypes() {
        if (listTypes == null) {
            listTypes = new ArrayList<String>();
            listTypes.add(Clazz._TYPE_SYSTEM_USER._ADMIN_CONFIG);
            listTypes.add(Clazz._TYPE_SYSTEM_USER._ADMIN_USER);
//            listTypes.add(Clazz._TYPE_SYSTEM_USER._EMPRESA);
//            listTypes.add(Clazz._TYPE_SYSTEM_USER._SOCIO);
        }
        return listTypes;
    }

    public String getTypeCompany() {
        return typeCompany;
    }

    public void setTypeCompany(String typeCompany) {
        this.typeCompany = typeCompany;
    }

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public void setListTypes(List<String> listTypes) {
        this.listTypes = listTypes;
    }

    public void setCompanyService(ICompanyService companyService) {
        this.companyService = companyService;
    }

    public void setSystemUserService(ISystemUserService systemUserService) {
        this.systemUserService = systemUserService;
    }

    public List<SystemUser> getListSystemUser() throws Exception {
        if (listSystemUser == null) {
            String hql = "from SystemUser s where "
                    + "s.type='" + Clazz._TYPE_SYSTEM_USER._ADMIN_CONFIG + "' "
                    + "or s.type='" + Clazz._TYPE_SYSTEM_USER._ADMIN_USER + "'";
            listSystemUser = systemUserService.listHQL(hql);
        }
        return listSystemUser;
    }

    public void setListSystemUser(List<SystemUser> listSystemUser) {
        this.listSystemUser = listSystemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    // </editor-fold> 
}
